<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Proxy的this问题</title>
</head>
<body>
    <h1>Proxy的this问题</h1>

    <script>
        const target = {
            m: function() {
                console.log(this === proxy);
            }
        };
        const handler = {};

        const proxy = new Proxy(target, handler);

        target.m(); // false
        proxy.m();  // true

        // this绑定的例子
        const _name = new WeakMap();

        class Person {
            constructor(name) {
                _name.set(this, name);
            }
            get name() {
                return _name.get(this);
            }
        }

        const jane = new Person('Jane');
        const proxy2 = new Proxy(jane, {});
        console.log(jane.name); // 'Jane'
        // console.log(proxy2.name); // undefined，因为this指向proxy2
    </script>
</body>
</html> 